package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.fastdfs.FastDFSClient;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmMaterialDto;
import com.usian.model.media.dtos.WmUserDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmNewsMaterial;
import com.usian.model.media.pojos.WmUser;
import com.usian.utils.threadlocal.WmThreadLocalUtils;
import com.usian.wemedia.mapper.WmMaterialMapper;
import com.usian.wemedia.mapper.WmNewsMaterialMapper;
import com.usian.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

/**
 * ClassName: WmMaterialServiceImpl
 * Author:   WGG
 * Date:    2022-11-2022/11/16-18:54
 * Version: 1.0
 * Description:
 */
@Service
@Slf4j
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {


    @Autowired
    private FastDFSClient fastDFSClient;

    @Value("${fdfs.url}")
    private String fileUrl;

    @Resource
    private WmMaterialMapper wmMaterialMapper;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //判断参数是否为空
        if (multipartFile==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //将图片存入fastdfs
        String file = null;
        try {
            file = fastDFSClient.uploadFile(multipartFile);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR);
        }
        //同时将图片路径名称的存入wm_material表
        WmMaterial material = new WmMaterial();
        material.setCreatedTime(new Date());
        //在ThreadLocal线程中拿到id
        log.info("==================================={}", WmThreadLocalUtils.getUser());
        WmUser user = WmThreadLocalUtils.getUser();
        material.setUserId(user.getApUserId());
        //设置是否收藏 否
        material.setIsCollection(0);
        //图片类型 0 图片  1 视频
        material.setType(0);
        //图片完整路径
        material.setUrl(fileUrl+file);
        //保存
        save(material);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult list(WmMaterialDto dto) {
        //      校验参数是否为空
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //		校验分页数据是否正确，初始化分页
        dto.checkParam();
        //		设置查询条件判断isCollection是否为空，等值查询
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        //查找对应用户的
        Integer userId = WmThreadLocalUtils.getUser().getApUserId();
        queryWrapper.eq(WmMaterial::getUserId,userId);
//        if (dto.getIsCollection()!=0) {
            queryWrapper.eq(dto.getIsCollection() != null, WmMaterial::getIsCollection, dto.getIsCollection());
//        }
        Page pageInfo = new Page(dto.getPage(),dto.getSize());
        IPage page = page(pageInfo, queryWrapper);
        //		响应数据
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult delPicture(Integer id) {
        //校验参数
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据material_id判断WmNewsMaterial中是否存在是否可以被删除
        WmMaterial wmMaterial = wmMaterialMapper.selectOne(Wrappers.<WmMaterial>lambdaQuery().eq(WmMaterial::getId,id));
        if (wmMaterial.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        //删除
        wmMaterialMapper.deleteById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult collectOrCancelCollect(Integer id, Integer type) {
        //校验参数是否为空
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //设置修改条件
        LambdaUpdateWrapper<WmMaterial> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.set(WmMaterial::getIsCollection,type);
        updateWrapper.eq(WmMaterial::getId,id);
        //执行修改
        update(updateWrapper);
        //响应结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
